<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <router-link to="/home">主页</router-link>
    <router-link to="/product/123">产品详情1</router-link>
    <router-link to="/product/456">产品详情2</router-link>
    <router-view></router-view>
  </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>
  let routes = [{
      path: "/home",
      beforeEnter: (to, from, next) => {
        console.log("/home 路由的导航守卫");
        next();
      },
      component: () =>
        Promise.resolve({
          template: `<h1>主页</h1>`,
          // 路由进入之前
          beforeRouteEnter(to, from, next) {
            console.log("组件导航守卫 进入到主页的路由");
            //   console.log(this); // => undefined 现在还不可以访问到 实例
            next();
          },
          // 路由更新的时候
          // product/123 => product/456
          beforeRouteUpdate(to, from, next) {
            console.log("组件导航守卫  主页 路由更新的时候");
            //   console.log(this); // => 产品详情组件
            next();
          },
          // 路由离开的时候
          beforeRouteLeave(to, from, next) {
            console.log("组件导航守卫  离开 /home 路由");
            //   console.log("现在的路由", from);
            //   console.log("前往的路由", to);
            //   console.log(this);
            next();
          },
        }),
    },
    {
      path: "/product/:productId",
      beforeEnter: (to, from, next) => {
        console.log("/product 路由的导航守卫");
        next();
      },
      component: () => import("./组件内的导航路由.js"),
    },
  ];
  let router = new VueRouter({
    routes,
  });
  /* 1. 全局前置导航守卫 */
  router.beforeEach((to, from, next) => {
    console.log("执行 全局的 前置的导航守卫");
    next();
    // to and from are both route objects. must call `next`.
  });
  let vm = new Vue({
    router,
    el: "#app",
  });
</script>

显示组件的离开导航
全局前置导航守卫

</html>